package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.ScoreMapper;
import mianyang.zhangsan.com.mapper.ScoreStatsMapper;
import mianyang.zhangsan.com.pojo.Score;
import mianyang.zhangsan.com.pojo.ScoreCustom;
import mianyang.zhangsan.com.pojo.ScoreStats;
import mianyang.zhangsan.com.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("scoreService")
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private ScoreStatsMapper scoreStatsMapper;

    /**
     * 根据条件分页查询
     * @param page
     * @param size
     * @param studentId
     * @param courseId
     * @return
     */
    @Override
    public Page<Score> queryScoreByPage(int page, int size, int studentId, int courseId) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        if (studentId != 0){
            wrapper.eq("student_id", studentId);
        }
        if (courseId != 0){
            wrapper.eq("course_id", courseId);
        }
        if (studentId == 0 && courseId == 0){
            return scoreMapper.selectPage(new Page<>(page, size), null);
        }
        return scoreMapper.selectPage(new Page<>(page, size), wrapper);

    }

    /**
     * 封装成绩信息，用于页面展示
     * @param score
     * @param username
     * @param name
     * @return
     */
    @Override
    public ScoreCustom scorePackage(Score score, String username, String name) {
        ScoreCustom custom = new ScoreCustom();
        custom.setId(score.getId());
        custom.setUsername(username);
        custom.setCourse(name);
        custom.setScore(score.getScore());
        custom.setRemark(score.getRemark());
        return custom;
    }

    /**
     * 根据studentId查询成绩信息
     * @param studentId
     * @return
     */
    @Override
    public List<Score> queryCourseByStudentId(Integer studentId) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        return scoreMapper.selectList(wrapper);
    }

    /**
     * 添加成绩信息
     * @param score
     */
    @Override
    public void scoreAdd(Score score) {
        scoreMapper.insert(score);
    }

    /**
     * 根据id删除成绩信息
     * @param id
     */
    @Override
    public void delete(int id) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        scoreMapper.delete(wrapper);
    }

    /**
     * 查询成绩的平均值等
     * @param courseId
     * @return
     */
    @Override
    public ScoreStats queryScoreStatsByCourseId(int courseId) {
        return scoreStatsMapper.queryScoreStats(courseId);
    }

    /**
     * 根据课程查询成绩
     * @param courseId
     * @return
     */
    @Override
    public List<Score> queryScoreByCourseId(int courseId) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        return scoreMapper.selectList(wrapper);
    }
}
